﻿@model 商品后台mvcdemo.Models.ProductInfo

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>新增商品</title>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
</head>
<body>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>商品表</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                    <span class="tsname">*</span>
                </div>

            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
                    <span id="tsname">*</span>
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.AddTime, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.AddTime, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.AddTime, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.TypeId, "商品类型", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("TypeId", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Proimage, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Proimage, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Proimage, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="新增" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @Html.ActionLink("返回", "Index")
    </div>

    <script>
               //商品的名称必填验证
               $("#Name").blur(function () {
                        var sgName = $("#Name").val();
                        if (sgName == "") {
                            $(".tsname").html("必填");
                            $(".tsname").css("color", "red");
                        }
                        else {
                            $(".tsname").html("");
                            $.ajax({
                                url: "YznameKey",
                                type: "get",
                                data: { "Name": sgName },
                                dataType: "json",
                                async: false,
                                success: function (result) {
                                    if (result == true) {
                                        $(".tsname").html("也有该水果名");
                                        $(".tsname").css("color", "red");
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            });
                        };
                });
                    //新增按钮验证是非空
               $("form").submit(function () {
                     return yzsfwk();
                }),
                function yzsfwk() {
                 var sgName = $("#Name").val();
                 var sgPrice = $("#Price").val();
                if(sgName == "") {
                    $(".tsname").html("必填");
                    $(".tsname").css("color", "red");
                } else if (sgPrice == "") {
                    $("#tsname").html("必填");
                    $("#tsname").css("color", "red");
                } else {
                    $(".tsname").html("");
                }
            };
    </script>
</body>
</html>
